function  loadRadAdminPage(contentDiv, action){
    $.ajax({
	      type:    "GET",
	      url:      "../RadAdmin/" + action + "?set_ajax_view",
	      dataType: "json",
	      complete: function(){
	          
	      },
	      success:    function(data) {
	          if (data.error.code == 'OK') {
	              $('#' + contentDiv).html(data.result);
	          }
	          else {
	              displayError(data.error.mess);              
	          }
	      }
    });
}

/////////////////  Rad services ////////////////////
function editRadService(serviceId){
    serviceName =   $('#serviceName_' + serviceId).text();
    price       =   $('#price_' + serviceId).text();
    status      =   $('#disabled_' + serviceId).val();
    
    $('#editRadServiceFrm #serviceId').val(serviceId);
    $('#editRadServiceFrm #serviceName').val(serviceName);
    $('#editRadServiceFrm #price').val(price);
    $('#editRadServiceFrm input[name=\'disabled\']').removeAttr('checked');
    $('#editRadServiceFrm input[name=\'disabled\'][value=\'' + status + '\']').attr('checked', 'checked');
    $('#editRadServiceFrm #actionTitle').text(_('Edit service'));
}

function addNewRadService(){
    resetRadServiceEditForm();
}

function resetRadServiceEditForm() {
    $('#editRadServiceFrm #serviceId').val('');    
    $('#editRadServiceFrm #serviceName').val('');
    $('#editRadServiceFrm #price').val('');
    $('#editRadServiceFrm #radGroupLbx').val('');
    $('#editRadServiceFrm input[name=\'disabled\']').removeAttr('checked');
    $('#editRadServiceFrm input[name=\'disabled\'][value=\'0\']').attr('checked', 'checked');
    $('#editRadServiceFrm #addRadServiceMsg').text('');
    $('#editRadServiceFrm #actionTitle').text(_('Add service'));
    setInputCss('editRadServiceFrm');
}
function isReadyToSaveRadService() {
    if (!isNotEmpty($('#editRadServiceFrm #serviceName'), _('Empty service name'), 'addRadServiceMsg')) {
        return false;
    }
    
    var lcInt = locale(decimalPoint, thousandSep, 0);
    if (!validatePositiveIntegerByLocale($('#editRadServiceFrm #price'), lcInt, true, _('Invalid amount'), 'addRadServiceMsg')) {
        return false;
    }
    
    return true;
}

function saveRadServiceInfo() {
    if (!isReadyToSaveRadService()) {
        return;
    }
    showAjaxLoader('loaderDiv', 'actionBtnDiv', true);
    $.ajax({
          type:    "POST",
          url:      "../RadAdmin/saveRadServiceAjax?set_ajax_view",
          data:     $('#editRadServiceFrm').formSerialize(),
          dataType: "json",
          complete: function(){
              showAjaxLoader('loaderDiv', 'actionBtnDiv', false);
          },
          success:    function(data) {
              if (data.error.code == 'OK') {
                  if ($('#serviceId').val() > 0) {
                    mode    =   'update';
                  }
                  else {
                    mode    =   'new';
                  }
                  bindServiceData(data.result, mode);
                  resetRadServiceEditForm();
              }
              else {
                  displayError(data.error.mess);              
              }
          }
    });
}

function bindServiceData(data, mode) {
    if (mode == 'update') {
        $('#serviceName_' + data.serviceId).html(data.serviceName);
        $('#price_' + data.serviceId).text(data.price);
        $('#disabled_' + data.serviceId).val(data.disabled);
        $('#disabledText_' + data.serviceId).text(data.disabledText);
    }
    if (mode == 'new') {
        $('#radServiceListTbl tr#no_result').remove();
        index     =   $('#radServiceListTbl tbody tr').length + 1;
		newRow    =   "<tr id=\"row_" + data.serviceId + "\">"; 
		newRow    +=  "<td>" + index + "</td>";
		newRow    +=  "<td><a id='serviceName_" + data.serviceId + "' href='javascript:editRadService\(" + data.serviceId + "\)'>";
		newRow    +=  data.serviceName + "</td>";
		newRow    +=  "<td><span id='price_" + data.serviceId + "'>"  + data.price +"</span></td>";
		newRow    +=  "<td><span id='disabledText_" + data.serviceId + "'>"  + data.disabledText +"</span>";
		newRow    +=  "<input type='hidden' id='disabled_" + data.serviceId + "' value='" + data.disabled + "' /></td>";
		newRow    +=  "</tr>";
		
		$('#radServiceListTbl tbody').append(newRow);
    }
    setServiceTableCss();
}

function setServiceTableCss(){
    $('#radServiceListTbl tbody tr').removeClass('last');
    $('#radServiceListTbl tbody tr:last').addClass('last');
}   
/////////////////  End rad services ////////////////////

/////////////////  Rad groups ////////////////////
function resetRadGroupEditForm() {
    $('#editRadGroupFrm #radGroupId').val('');    
    $('#editRadGroupFrm #groupName').val('');
    $('#editRadGroupFrm #radTypeListLbx').val();
    $('#editRadGroupFrm #actionTitle').text(_('Add new group'));
    $('#editRadGroupFrm #addRadGroupMsg').text('');
    setInputCss('editRadGroupFrm');
}

function editRadGroup(radGroupId) {
    groupName   =   $('#groupName_' + radGroupId).text();
    radType   =   $('#radType_' + radGroupId).val();
    $('#editRadGroupFrm #radGroupId').val(radGroupId);    
    $('#editRadGroupFrm #groupName').val(groupName);
    $('#editRadGroupFrm #radTypeListLbx').val(radType);
    $('#editRadGroupFrm #actionTitle').text(_('Edit group'));
}

function isReadyToSaveGroup() {
	if (!isNotEmpty($('#editRadGroupFrm #groupName'), _('Empty group name'), 'addRadGroupMsg')) {
        return false;
    }
    return true;
}

function saveRadGroupInfo() {
	if (!isReadyToSaveGroup()) {
	   return;
	}
	showAjaxLoader('loaderDiv', 'actionBtnDiv', true);
	$.ajax({
          type:    "POST",
          url:      "../RadAdmin/saveRadGroupAjax?set_ajax_view",
          data:     $('#editRadGroupFrm').formSerialize(),
          dataType: "json",
          complete: function(){
              showAjaxLoader('loaderDiv', 'actionBtnDiv', false);
          },
          success:    function(data) {
              if (data.error.code == 'OK') {
                  if ($('#radGroupId').val() > 0) {
                    mode    =   'update';
                  }
                  else {
                    mode    =   'new';
                  }
                  bindGroupData(data.result, mode);
                  resetRadGroupEditForm();
              }
              else {
                  displayError(data.error.mess);              
              }
          }
    });
}

function bindGroupData(data, mode) {
    if (mode == 'update') {
        $('#groupName_' + data.groupId).html(data.groupName);
        $('#radType_' + data.groupId).val(data.radType);
        $('#radTypeName_' + data.groupId).text(data.radTypeName);
    }
    if (mode == 'new') {
        $('#radGroupListTbl tr#no_result').remove();
        index     =   $('#radGroupListTbl tbody tr').length + 1;
		newRow    =   "<tr class='last' id=\"row_" + data.groupId + "\">"; 
		newRow    +=  "<td>" + index + "</td>";
		newRow    +=  "<td><a id='groupName_" + data.groupId + "' href='javascript:editRadGroup\(" + data.groupId + "\)'>";
		newRow    +=  data.groupName + "</td>";
		newRow    +=  "<td><span id='radTypeName_" + data.groupId + "'>"  + data.radTypeName +"</span>";
		newRow    +=  "<input type='hidden' id='radType_" + data.groupId + "' value='" + data.radType +"'/></td>";
		newRow    +=  "</tr>";
		
		$('#radGroupListTbl tbody').append(newRow);
    }
    setGroupTableCss();
}

function addNewRadGroup() {
    resetRadGroupEditForm();
    //switchDiv('radGroupAdminButtonDiv', 'radGroupEditDiv');
}

function setGroupTableCss() {
    $('#radGroupListTbl tbody tr').removeClass('last');
    $('#radGroupListTbl tbody tr:last').addClass('last');
}

function setInputCss(containerId) {
    $('#' + containerId + ' input').css('border-color', '#AAA');
}

/////////////////  End rad groups ////////////////////

/////////////////  Service-group mapping ///////////////////
function getGroupServices(){
	if ($('#radGroupLbx').val() == null || $('#radGroupLbx').val()==0) {
		$('#radServiceContentDiv').html('').hide();
		return;
	}
    showAjaxLoader('loaderDiv', 'dummy', true);
    $.ajax({
        type:   "GET",
        url:    "../RadAdmin/getGroupServicesAjax?set_ajax_view",
        data:   "radGroupId=" + $('#radGroupLbx').val(),
        dataType: "json",
        beforeSend: function() {
                    
        },
        complete:   function() {
            showAjaxLoader('loaderDiv', 'dummy', false);
        },
        success:    function(data) {
              if (data.error.code == 'OK') {
                  $('#radServiceContentDiv').html(data.result).show();
              }
              else {
                  displayError(data.error.mess);              
              }
          }
    });
    
}

function moveItem(option) {
	var selectedServiceIds = new Array();
	var radGroupId = $("#radGroupId").val();
	var from;
	var to;
    if (option == 'add') {
        //Append selected services to mapped service list
       selectedServiceIds = $("#unMappedServiceLbx").selectedValues();
       from = "unMappedServiceLbx";   
       to = "mappedServiceLbx"; 
    } else if (option == 'remove') {
        //Remove selected services to mapped service list
        selectedServiceIds = $("#mappedServiceLbx").selectedValues();
        from = "mappedServiceLbx";   
        to = "unMappedServiceLbx"; 
        
    }
    if(selectedServiceIds == '' || selectedServiceIds == null){ 
    	return false;
    }
    showAjaxLoader('loaderDiv', 'dummy', true);
    $.ajax({
        type:   "POST",
        url:    "../RadAdmin/addServicesToGroup?set_ajax_view",
        data:   "option=" + option + "&radGroupId=" + radGroupId + "&selectedSeviceIds=" + selectedServiceIds,
        dataType: "json",
        beforeSend: function() {           
        },
        complete:   function() {
        	showAjaxLoader('loaderDiv', 'dummy', false);
        },
        success:    function(data) {
        	 if (data.error.code == 'OK') {
             	$("#" + from).copyOptions("#" + to);
             	$("#" + from).removeOption(/./, true);
             }
             else {
                 displayError(data.error.mess);              
             }
        }
    });      
}
/////////////////  End service-group mapping ///////////////////

/////////////////  Additional service ///////////////////
function saveAdditionalServiceInfo() {
    if (!isReadyToSaveAddService()) {
       return;
    }
    showAjaxLoader('loaderDiv', 'actionBtnDiv', true);
    $.ajax({
          type:    "POST",
          url:      "../RadAdmin/saveAddServiceAjax?set_ajax_view",
          data:     $('#editRadAddServiceFrm').formSerialize(),
          dataType: "json",
          complete: function(){
              showAjaxLoader('loaderDiv', 'actionBtnDiv', false);
          },
          success:    function(data) {
              if (data.error.code == 'OK') {
                  if ($('#serviceId').val() > 0) {
                    mode    =   'update';
                  }
                  else {
                    mode    =   'new';
                  }
                  bindAddServiceData(data.result, mode);
                  resetRadAddServiceEditForm();
              }
              else {
                  displayError(data.error.mess);              
              }
          }
    });
}

function isReadyToSaveAddService() {
    if (!isNotEmpty($('#editRadAddServiceFrm #serviceName'), _('Empty service name'), 'addRadAddServiceMsg')) {
        return false;
    }
    
    var lcInt = locale(decimalPoint, thousandSep, 0);
    if (!validatePositiveIntegerByLocale($('#editRadAddServiceFrm #price'), lcInt, true, _('Invalid amount'), 'addRadAddServiceMsg')) {
        return false;
    }
    
    return true;
}

function bindAddServiceData(data, mode) {
    if (mode == 'update') {
        $('#serviceName_' + data.serviceId).html(data.serviceName);
        $('#price_' + data.serviceId).text(data.price);
        $('#enabled_' + data.serviceId).val(data.enabled);
        $('#enabledText_' + data.serviceId).text(data.enabledText);
        $('#radType_' + data.serviceId).val(data.radType);
        $('#radTypeName_' + data.serviceId).text(data.radTypeName);
    }
    if (mode == 'new') {
        $('#radAddServiceListTbl tr#no_result').remove();
        index     =   $('#radAddServiceListTbl tbody tr').length + 1;
        newRow    =   "<tr id=\"row_" + data.serviceId + "\">"; 
        newRow    +=  "<td>" + index + "</td>";
        newRow    +=  "<td><a id='serviceName_" + data.serviceId + "' href='javascript:editRadAddService\(" + data.serviceId + "\)' title='"+_('Edit')+"'>";
        newRow    +=  data.serviceName + "</td>";
        newRow    +=  "<td><span id='price_" + data.serviceId + "'>"  + data.price +"</span></td>";
        newRow    +=  "<td><span id='radTypeName_" + data.serviceId + "'>"  + data.radTypeName +"</span>";
        newRow    +=  "<input type='hidden' id='radType_" + data.serviceId + "' value='" + data.radType +"'/></td>";
        newRow    +=  "<td><span id='enabledText_" + data.serviceId + "'>"  + data.enabledText +"</span>";
        newRow    +=  "<input type='hidden' id='enabled_" + data.serviceId + "' value='" + data.enabled + "' /></td>";
        newRow    +=  "</tr>";
        
        $('#radAddServiceListTbl tbody').append(newRow);
    }
    setRadServiceTableCss();
}

function setRadServiceTableCss(){
    $('#radAddServiceListTbl tbody tr').removeClass('last');
    $('#radAddServiceListTbl tbody tr:last').addClass('last');
}

function resetRadAddServiceEditForm() {
    $('#editRadAddServiceFrm #serviceId').val('');    
    $('#editRadAddServiceFrm #serviceName').val('');
    $('#editRadAddServiceFrm #price').val('');
    $('#editRadAddServiceFrm #radTypeLbx').val('');
    $('#editRadAddServiceFrm input[name=\'enabled\']').removeAttr('checked');
    $('#editRadAddServiceFrm input[name=\'enabled\'][value=\'1\']').attr('checked', 'checked');
    $('#editRadAddServiceFrm #addRadAddServiceMsg').text('');
    $('#editRadAddServiceFrm #actionTitle').text(_('Add service'));
    setInputCss('editRadAddServiceFrm');
}

function editRadAddService(serviceId) {
    serviceName =   $('#serviceName_' + serviceId).text();
    price       =   $('#price_' + serviceId).text();
    status      =   $('#enabled_' + serviceId).val();
    radType     =     $('#radType_' + serviceId).val();
    
    $('#editRadAddServiceFrm #serviceId').val(serviceId);
    $('#editRadAddServiceFrm #serviceName').val(serviceName);
    $('#editRadAddServiceFrm #price').val(price);
    $('#editRadAddServiceFrm #radTypeLbx').val(radType);
    $('#editRadAddServiceFrm input[name=\'enabled\']').removeAttr('checked');
    $('#editRadAddServiceFrm input[name=\'enabled\'][value=\'' + status + '\']').attr('checked', 'checked');
    $('#editRadAddServiceFrm #actionTitle').text(_('Edit service'));
}
///////////////// End additional service ///////////////////